﻿

#region References

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Freeform;

#endregion



namespace Space_in_Shaders
{
    public class Overlay : World
    {

        #region Members

        public SpaceWorld SpaceWorld { get; set; }

        public Entity ScoreDisplay;
        public Entity HealthDisplay;
        public Entity HelpDisplay;

        private Vector2 Spacing { get; set; }
        private Vector2 ItemLocation { get; set; }

        #endregion


        #region Init

        public Overlay(SpaceWorld SpaceWorld)
            : base(SpaceWorld.Game)
        {
            BackgroundColour = new Color(Vector4.Zero);
            this.SpaceWorld = SpaceWorld;

            // Create viewport.
            Viewport2D View = new Viewport2D(this);
            View.SpatialProperties.Size = new Vector2(1, 0.1f);
            View.SpatialProperties.Position = Vector2.Zero;
            View.Camera.SpatialProperties.Size = new Vector2(1024, 76.8f);
            View.Depth = 0.7f;

            // Setup item locations and spacings.
            ItemLocation = new Vector2(100, 20);
            Spacing = new Vector2(350, 0);

            // Create the score display item.
            MakeItem(ref ScoreDisplay, "Score: 0");

            // Create the health display item.
            MakeItem(ref HealthDisplay, "Health: 0");

            // Create the entity count display item.
            MakeItem(ref HelpDisplay, "WSAD (Move)  MouseButtons (Fire)");
        }

        #endregion


        public override void Update(GameTime time)
        {
            base.Update(time);

            // Update Score
            (ScoreDisplay.Actor as Actor_Text2D).Text = "Score: " + SpaceWorld.Score;

            Vector4 ScoreColour = ScoreDisplay.Actor.ColourOverlay.ToVector4();
            ScoreColour += (Vector4.One - ScoreColour) * 0.02f;
            ScoreDisplay.Actor.ColourOverlay = new Color(ScoreColour);


            // Update HP
            Int32 HP = SpaceWorld.Player.Health;
            if(SpaceWorld.Player.Health < 0)
                (HealthDisplay.Actor as Actor_Text2D).Text = "Health: Dead";
            else
                (HealthDisplay.Actor as Actor_Text2D).Text = "Health: " + HP;

            HealthDisplay.Actor.ColourOverlay = new Color(new Vector4(1 - HP/20f, HP/20f, 0, 1));


            // Displays number of entities in SpaceWorld.
            if (SpaceWorld.Player.IsDestroyed)
                (HelpDisplay.Actor as Actor_Text2D).Text = "Spacebar (Respawn)";
            else
                (HelpDisplay.Actor as Actor_Text2D).Text = "WSAD (Move)  MouseButtons (Fire)";
        }


        #region Implementation

        private void MakeItem(ref Entity Item, String Text)
        {
            Item = new Entity(this);
            Item.SpatialProperties.Position = ItemLocation;
            ItemLocation += Spacing;
            Actor_Text2D A = new Actor_Text2D(Item, Text, Game.Content.Load<SpriteFont>("Fonts/Overlay"));
            A.UseNaturalFontSize = true;
            A.ColourOverlay = Color.White;
        }

        #endregion
    }
}